public class testPreventDuplicateEmailForContact {
	
	//This is a testing class for preventDuplicateEmailForContact
	 public static testmethod void t1() {
		
		string emailToCheck = 'jdoe@test.com'; 
		string emailOtherEmail = 'other@test.com';
		
		
		LINKSYS_SYS_PARAM__c sysParamObj = [Select VALUE__c from LINKSYS_SYS_PARAM__c
        	Where   PARAM_NAME__c = 'PreventDuplicateEmailTrigger' ];
		
		System.assertNotEquals(null, sysParamObj);
		
		string shouldRun = sysParamObj.VALUE__C.toUpperCase();
		
		if (shouldRun != 'ENABLED')
			return;
		
		System.assertNotEquals('', shouldRun);
		
		integer contactCount = [select count() from Contact where Email = :emailToCheck];

		System.assertEquals(0, contactCount);
		
		Contact ct = new Contact(FirstName = 'John', LastName = 'Doe', Email = emailToCheck);

		insert ct;

		contactCount = [select count() from Contact where Email = :emailToCheck];

		System.assertEquals(1, contactCount);

		try
		{
			System.debug('Testing duplicate insert');
			Contact ct1 = new Contact(FirstName = 'Jane', LastName = 'Doe', Email = emailToCheck);

			insert ct1;
		}
		catch (System.DmlException e) {
    		for (Integer i = 0; i < e.getNumDml(); i++) {
        		// Process exception here
        		System.debug(e.getDmlMessage(i));
			}
		}

		System.debug('Testing duplicate update');
		Contact ct1 = new Contact(FirstName = 'Jane', LastName = 'Doe', Email = emailOtherEmail);
		insert ct1;

		try
		{
			ct1.Email = emailToCheck;

			update ct1;
		}
		catch (System.DmlException e) {
    		for (Integer i = 0; i < e.getNumDml(); i++) {
        		// Process exception here
        		System.debug(e.getDmlMessage(i));
			}
		}
		
		contactCount = [select count() from Contact where Email = :emailToCheck];


		System.assertEquals(1, contactCount);
    }
	
}